package com.yunwaikeji.gy_tool_library.widget.boxDialog;

import android.content.Context;
import android.graphics.Color;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYBoxDialogInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;
import com.yunwaikeji.gy_tool_library.view.GYTextView;
import com.yunwaikeji.gy_tool_library.view.GYView;
import com.yunwaikeji.gy_tool_library.widget.GYButton;
import com.yunwaikeji.gy_tool_library.widget.GYDialog;
import com.yunwaikeji.gy_tool_library.widget.recycler.GYRecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYBoxDialog extends GYDialog implements GYBoxDialogInfo {
    public GYBoxDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public GYBoxDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected GYBoxDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private GYLinearLayout titleLL, allLL, contentLL;
    private GYRecyclerView bottomClickRv;
    private GYTextView titleTextView, contentTextView;
    private GYView dividerTopView, dividerCenterView;

    private final GYBoxDialogAdapter boxDialogAdapter = new GYBoxDialogAdapter(this);
    private final List<GYButton> allButtonList = new ArrayList<>();

    private int clickDividerPx = 1;
    private int clickDividerColor = Color.DKGRAY;
    private int clickBackgroundColor = Color.WHITE;
    private int clickPressColor = Color.LTGRAY;

    private void init() {
        setContentView(R.layout.dialog_gy_box);
        getWindow().setBackgroundDrawable(GYDisplayUtils.newBuilder()
                .setNormalColor(Color.TRANSPARENT)
                .buildDrawable());
        titleLL = findViewById(R.id.titleLL);
        allLL = findViewById(R.id.allLL);
        contentLL = findViewById(R.id.contentLL);
        titleTextView = findViewById(R.id.titleTextView);
        contentTextView = findViewById(R.id.contentTextView);
        dividerTopView = findViewById(R.id.dividerTopView);
        dividerCenterView = findViewById(R.id.dividerCenterView);
//        clickLL = findViewById(R.id.bottomClickLL);
        bottomClickRv = findViewById(R.id.bottomClickRv);
        bottomClickRv.setAdapter(boxDialogAdapter);
        setClickDividerPx(clickDividerPx);
        setClickDividerColor(clickDividerColor);
        setSizeRatioFromWidthProportion(0.7f, 5, 3);
        setWeight(2, 6, 3);
        getAllLinearLayout().getBackgroundBuilder().setRadiusDp(8).build();
        setDividerTopColor(Color.LTGRAY);
        setDividerCenterColor(Color.LTGRAY);
    }

    @Override
    public void show() {
        refreshClickRecyclerView();
        super.show();
    }

    public GYBoxDialog setTitleHeightPx(int titleHeightPx) {
        titleTextView.getViewSet().setWeight(0);
        titleTextView.getViewSet().setHeightPx(titleHeightPx);
        return this;
    }

    public GYBoxDialog setTitleHeightDp(int titleHeightDp) {
        titleLL.getViewSet().setWeight(0);
        titleLL.getViewSet().setHeightDp(titleHeightDp);
        return this;
    }

    public GYBoxDialog setContentHeightPx(int contentHeightPx) {
        contentLL.getViewSet().setWeight(0);
        contentLL.getViewSet().setHeightPx(contentHeightPx);
        return this;
    }

    public GYBoxDialog setContentHeightDp(int contentHeightDp) {
        contentLL.getViewSet().setWeight(0);
        contentLL.getViewSet().setHeightDp(contentHeightDp);
        return this;
    }

    public GYBoxDialog setClickHeightPx(int clickHeightPx) {
        bottomClickRv.getViewSet().setWeight(0);
        bottomClickRv.getViewSet().setHeightPx(clickHeightPx);
        return this;
    }

    public GYBoxDialog setClickHeightDp(int clickHeightDp) {
        bottomClickRv.getViewSet().setWeight(0);
        bottomClickRv.getViewSet().setHeightDp(clickHeightDp);
        return this;
    }

    public GYBoxDialog setDividerTopPx(int dividerTopPx) {
        dividerTopView.getViewSet().setHeightPx(dividerTopPx);
        return this;
    }

    public GYBoxDialog setDividerTopColor(int dividerTopColor) {
        dividerTopView.getBackgroundBuilder().setBackgroundColor(dividerTopColor).build();
        return this;
    }

    public GYBoxDialog setDividerTopDp(int dividerTopDp) {
        dividerTopView.getViewSet().setHeightDp(dividerTopDp);
        return this;
    }

    public GYBoxDialog setDividerCenterPx(int dividerCenterPx) {
        dividerCenterView.getViewSet().setHeightPx(dividerCenterPx);
        return this;
    }

    public GYBoxDialog setDividerCenterColor(int dividerCenterColor) {
        dividerCenterView.getBackgroundBuilder().setBackgroundColor(dividerCenterColor).build();
        return this;
    }

    public GYBoxDialog setDividerCenterDp(int dividerCenterDp) {
        dividerCenterView.getViewSet().setHeightDp(dividerCenterDp);
        return this;
    }

    public GYBoxDialog setShowTitle(boolean showTitle) {
        if (showTitle) {
            titleLL.setVisibility(View.VISIBLE);
            dividerTopView.setVisibility(View.VISIBLE);
        } else {
            titleLL.setVisibility(View.GONE);
            dividerTopView.setVisibility(View.GONE);
        }
        return this;
    }

    private OnBottomClickListener onBottomAllClickListener;
    private OnBottomClickListener onBottomShowClickListener;

    public interface OnBottomClickListener {
        boolean onClick(GYBoxDialog boxDialog, GYButton button, int position);
    }

    public GYBoxDialog setOnBottomAllClickListener(OnBottomClickListener onBottomAllClickListener) {
        this.onBottomAllClickListener = onBottomAllClickListener;
        return this;
    }

    public GYBoxDialog setOnBottomShowClickListener(OnBottomClickListener onBottomShowClickListener) {
        this.onBottomShowClickListener = onBottomShowClickListener;
        return this;
    }

    public GYBoxDialog setClickStrings(String... strings) {
        return setClickStrings(Arrays.asList(strings));
    }

    public GYBoxDialog setClickStrings(List<String> stringList) {
        return addClickStrings(stringList);
    }

    public GYBoxDialog addClickStrings(String... strings) {
        return addClickStrings(Arrays.asList(strings));
    }

    public GYBoxDialog addClickStrings(List<String> stringList) {
        return addClickStrings(-1, stringList);
    }

    public GYBoxDialog addClickStrings(int index, String... strings) {
        return addClickStrings(index, Arrays.asList(strings));
    }

    public GYBoxDialog addClickStrings(int index, List<String> stringList) {
        List<GYButton> buttonList = new ArrayList<>();
        for (String click : stringList) {
            GYButton button = new GYButton(getContext()).setText(click);
            button.getBackgroundBuilder()
                    .setBackgroundColor(clickBackgroundColor)
                    .setChangeColor(clickPressColor)
                    .build();
            buttonList.add(button);
        }
        return addClickButtons(index, buttonList);
    }

    public GYBoxDialog setClickButtons(GYButton... buttons) {
        return setClickButtons(Arrays.asList(buttons));
    }

    public GYBoxDialog setClickButtons(List<GYButton> buttonList) {
        boxDialogAdapter.setData();
        return addClickButtons(buttonList);
    }

    public GYBoxDialog addClickButtons(GYButton... buttons) {
        return addClickButtons(Arrays.asList(buttons));
    }

    public GYBoxDialog addClickButtons(List<GYButton> buttonList) {
        return addClickButtons(-1, buttonList);
    }

    public GYBoxDialog addClickButtons(int index, GYButton... buttons) {
        return addClickButtons(index, Arrays.asList(buttons));
    }

    public GYBoxDialog addClickButtons(final int index, List<GYButton> buttonList) {
        if (index < 0 || index >= allButtonList.size()) {
            allButtonList.addAll(buttonList);
        } else {
            int position = index;
            for (GYButton button : buttonList) {
                allButtonList.add(position, button);
                position++;
            }
        }
        return refreshClickRecyclerView();
    }

    private GYBoxDialog refreshClickRecyclerView() {
        List<GYButton> showList = new ArrayList<>();
        int showPosition = -1;
        for (int i = 0; i < allButtonList.size(); i++) {
            GYButton button = allButtonList.get(i);
            boolean isShow = button.getVisibility() == View.VISIBLE || button.getVisibility() == View.INVISIBLE;
            if (isShow) {
                showList.add(button);
                showPosition++;
            }
            int finalI = i;
            int finalShowPosition = showPosition;
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onBottomAllClickListener != null) {
                        if (!onBottomAllClickListener.onClick(GYBoxDialog.this, button, finalI)) {
                            dismiss();
                        }
                    }
                    if (onBottomShowClickListener != null && isShow) {
                        if (!onBottomShowClickListener.onClick(GYBoxDialog.this, button, finalShowPosition)) {
                            dismiss();
                        }
                    }
                }
            });
        }
        boxDialogAdapter.setData(showList);
        bottomClickRv.getRecyclerBuilder().setSpanCount(showList.size()).build();
        return this;
    }

    public List<GYButton> getAllButtonList() {
        return allButtonList;
    }

    public List<GYButton> getShowButtonList() {
        List<GYButton> buttonList = new ArrayList<>();
        for (GYButton button : getAllButtonList()) {
            if (button.getVisibility() == View.VISIBLE || button.getVisibility() == View.INVISIBLE) {
                buttonList.add(button);
            }
        }
        return buttonList;
    }

    public GYBoxDialog setClickPressColor(int pressColor) {
        return setClickColor(UNCHANGED, pressColor);
    }

    public GYBoxDialog setClickBackgroundColor(int backgroundColor) {
        return setClickColor(backgroundColor, UNCHANGED);
    }

    public GYBoxDialog setClickColor(int backgroundColor, int pressColor) {
        for (GYButton button : getAllButtonList()) {
            if (backgroundColor != UNCHANGED) {
                this.clickBackgroundColor = backgroundColor;
                button.getBackgroundBuilder().setBackgroundColor(backgroundColor).build();
            }
            if (pressColor != UNCHANGED) {
                this.clickPressColor = pressColor;
                button.getBackgroundBuilder().setChangeColor(pressColor).build();
            }
        }
        return this;
    }

    public GYBoxDialog setClickDividerPx(int clickDividerPx) {
        this.clickDividerPx = clickDividerPx;
        bottomClickRv.getRecyclerBuilder().setDividerSizePx(clickDividerPx).build();
        return this;
    }

    public GYBoxDialog setClickDividerColor(int clickDividerColor) {
        this.clickDividerColor = clickDividerColor;
        bottomClickRv.getRecyclerBuilder().setDividerColor(clickDividerColor).build();
        return this;
    }

    public GYLinearLayout getContentLL() {
        return contentLL;
    }

    public GYTextView getContentTextView() {
        return contentTextView;
    }

    public GYTextView getTitleTextView() {
        return titleTextView;
    }

    public GYView getDividerTopView() {
        return dividerTopView;
    }

    public GYView getDividerCenterView() {
        return dividerCenterView;
    }

    public GYView getDividerBottomView() {
        return dividerCenterView;
    }

    public GYRecyclerView getBottomClickRecyclerView() {
        return bottomClickRv;
    }

    public GYLinearLayout getAllLinearLayout() {
        return allLL;
    }

    public GYBoxDialog setTitleText(String text) {
        titleTextView.setText(text);
        return this;
    }

    public GYBoxDialog setContentStrings(String... contents) {
        return setContentStrings(Arrays.asList(contents));
    }

    public GYBoxDialog setContentStrings(List<String> contentList) {
        contentTextView.setText("");
        return addContents(contentList);
    }

    public GYBoxDialog addContents(String... contents) {
        return addContents(Arrays.asList(contents));
    }

    public GYBoxDialog addContents(List<String> contentList) {
        StringBuilder content = new StringBuilder(contentTextView.getViewSet().getTextString());
        for (String addContent : contentList) {
            content.append(addContent);
        }
        contentTextView.setText(content);
        return this;
    }

    public GYBoxDialog setWeight(float titleWeight, float contentWeight, float clickWeight) {
        titleLL.getViewSet().setSizePx(MATCH_PARENT, 0, titleWeight);
        contentLL.getViewSet().setSizePx(MATCH_PARENT, 0, contentWeight);
        bottomClickRv.getViewSet().setSizePx(MATCH_PARENT, 0, clickWeight);
        return this;
    }

    public GYBoxDialog setSizePx(int widthPx, int heightPx) {
        super.setSizePx(widthPx, heightPx);
        return this;
    }

    public GYBoxDialog setWidthDp(int widthDp) {
        super.setWidthDp(widthDp);
        return this;
    }

    public GYBoxDialog setHeightPx(int heightPx) {
        super.setHeightPx(heightPx);
        return this;
    }

    public GYBoxDialog setWidthPx(int widthPx) {
        super.setWidthPx(widthPx);
        return this;
    }

    public GYBoxDialog setSizeDp(int widthDp, int heightDp) {
        super.setSizeDp(widthDp, heightDp);
        return this;
    }

    public GYBoxDialog setHeightDp(int heightDp) {
        super.setHeightDp(heightDp);
        return this;
    }

    public GYBoxDialog setHeightProportion(float heightProportion) {
        super.setHeightProportion(heightProportion);
        return this;
    }

    public GYBoxDialog setSizeProportion(float width, float height) {
        super.setSizeProportion(width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromHeight(int width, int height) {
        super.setSizeRatioFromHeight(width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromHeightDp(int heightDp, int width, int height) {
        super.setSizeRatioFromHeightDp(heightDp, width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromHeightPx(int heightPx, int width, int height) {
        super.setSizeRatioFromHeightPx(heightPx, width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromWidth(int width, int height) {
        super.setSizeRatioFromWidth(width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromWidthDp(int widthDp, int width, int height) {
        super.setSizeRatioFromWidthDp(widthDp, width, height);
        return this;
    }

    public GYBoxDialog setSizeRatioFromWidthPx(int widthPx, int width, int height) {
        super.setSizeRatioFromWidthPx(widthPx, width, height);
        return this;
    }

    public GYBoxDialog setWidthProportion(float widthProportion) {
        super.setWidthProportion(widthProportion);
        return this;
    }

    public GYBoxDialog setOnCancelListener1(@Nullable OnCancelListener listener) {
        super.setOnCancelListener1(listener);
        return this;
    }

    public GYBoxDialog setOnDismissListener1(@Nullable OnDismissListener listener) {
        super.setOnDismissListener1(listener);
        return this;
    }

    public GYBoxDialog setOnKeyListener1(@Nullable OnKeyListener onKeyListener) {
        super.setOnKeyListener1(onKeyListener);
        return this;
    }

    public GYBoxDialog setOnShowListener1(@Nullable OnShowListener listener) {
        super.setOnShowListener1(listener);
        return this;
    }

    public GYBoxDialog setCancelable1(boolean flag) {
        super.setCancelable1(flag);
        return this;
    }

    public GYBoxDialog setCancelMessage1(@Nullable Message msg) {
        super.setCancelMessage1(msg);
        return this;
    }

    public GYBoxDialog setCanceledOnTouchOutside1(boolean cancel) {
        super.setCanceledOnTouchOutside1(cancel);
        return this;
    }

    public GYLinearLayout getTitleLL() {
        return titleLL;
    }
}
